// pages/fertility/fertility.js
const app = getApp()
import {  getpdf,getPrenatalCare } from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    setMealName:"顺产行政套餐",
    roomtype:"三天两晚/行政套房",
    setMealDetail:[],
    textareastr:undefined,
    price:0,
    body:1,
    text3:"重要提示 Important Information",
    text5:"查看产检套餐",
    text6:"",
    ldrpselect:false,
    selectedmultiple:false,
    LDPRcheck:false,
    updateselect:false,
    openinput:false,
    confirm:"确认",
    fangjianbiaozhun:"房间标准",
    enfangjianbiaozhun:"Room Type",
    huise:"灰色套餐不包含",
    jinse:"金色套餐升级项目",
    inputtext: [], 
    showinputtext:'', 
    language:"zh",
    showtext:"",
    information:[],
    deliverysetMealDetail:[],
    showprenatal:true,
    selectedPrenatal:false
  },

  /**
   * 生命周期函数--监听页面加载s
   */
  onLoad: function (options) {
    const that = this
  //   // console.log(options)
    getPrenatalCare({selectedserver:options.selectedserver,selectedsetMealIndex:options.selectedsetMealIndex}).then((res)=>{
      res.data.items.forEach(e=>{
        e.check = true
      })
      let str = ""
      if(app.globalData.language == "zh"){
        that.setData({
          language:app.globalData.language,
          setMealName:res.data.name,
          roomtype:res.data.roomtype,
          enRoomtype:res.data.enRoomtype,
          setMealDetail:res.data.items,
          selectedserver:options.selectedserver,
          setMealenName:res.data.enName,
        })
        let text6 = ""
      }else if(app.globalData.language == "en"){
        that.setData({
          language:app.globalData.language,
          setMealName:res.data.enName,
          fangjianbiaozhun:"Room Type",
          huise:"Not Included",
          jinse:"Update Item",
          roomtype:res.data.enRoomtype,
          text3:"Important Information",
          text5:"Prenatal Care Package",
          information:arr,
          infomations:options
        })
        this.setData({setMealDetail:res.data.items,selectedserver:options.selectedserver})
        let arr = res.data.infomation.map((e,index) =>{return String(index+1) + "." +e.en})
        str = arr.join("\n")
        // that.setData({textareastr:str})
      }
      let text6 = ""
      let text4 = ""
      if(app.globalData.language == "zh"){
        text4 = res.data.name
        if(options.selectedsetMealIndex==0){
          text6="查看行政套房"
        }else if(options.selectedsetMealIndex==1){
          text6="查看皇后套房"
        }else if(options.selectedsetMealIndex==2){
          text6="查看行政套房"
        }
      }else if(app.globalData.language == "en"){
        text4 = res.data.enName
        if(options.selectedsetMealIndex==0){
          text6="VIP Postpartum suite"
        }else if(options.selectedsetMealIndex==1){
          text6="VVIP Postpartum suite"
        }else if(options.selectedsetMealIndex==2){
          text6="VIP Postpartum suite"
        }
      }
      let arr = []
      res.data.infomation.forEach(e=>{
        let temp = {zh:e.zh,en:e.en}
        arr.push(temp)
      })
      let price = res.data.price
      that.setData({
        setMealDetail:res.data.items,
        // setMealName:res.data.name,
        price:price,
        text4:text4,
        selectedsetMealIndex:Number(options.selectedsetMealIndex),
        selectedweek:Number(options.selectedweek),
        // checkweekindex:checkweekindex,
        textareastr:str,
        showtext:str,
        text6:text6,
        information:arr,
        infomations:options
      })
    }),



    getPrenatalCare({selectedserver:"产检",selectedsetMealIndex:1}).then((res)=>{
      // console.log(res.data)
      let checkweekindex = 0
      // that.setData({deliverysetMealDetail:res.data.items})
      res.data.items.forEach((e,index)=>{
        // console.log(checkweekindex,index)
        if(checkweekindex>index){
          e.check = false
        }else{
          e.check = true
        }
        e.detail.forEach(i=>{
          if(i.includ.indexOf(0) == -1){
            i.check = false
          }else{
            i.check = true
          }
        })
      })
      // console.log(res.data)
      that.setData({
        deliverysetMealDetail:res.data.items
        // setMealDetail:res.data.items,
        // setMealName:res.data.name,
        // setMealenName:res.data.enName,
        // selectedsetMealIndex:Number(options.selectedsetMealIndex),
        // selectedweek:Number(options.selectedweek),
        // checkweekindex:checkweekindex,
      })
  })



  // },
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  hide(e){
    // this.setData({selectedIndex:-1})
    this.data.setMealDetail[e.currentTarget.dataset.index].check = false
    this.setData({
      setMealDetail:this.data.setMealDetail
    })
  },
  more(e){
    this.data.setMealDetail[e.currentTarget.dataset.index].check = true
    this.setData({
      setMealDetail:this.data.setMealDetail
    })
  },
  add(e){
    const that = this
    let temppeople = this.data.body + 1
    let tempprice = this.data.price + 20000
    if(this.data.selectedPrenatal){
      tempprice = tempprice + 7500
    }
    this.setData({body:temppeople,price:tempprice})
  },
  sub(e){
    const that = this
    if(this.data.body>1){
      let temppeople = this.data.body - 1
      let tempprice = this.data.price - 20000
      if(this.data.selectedPrenatal){
        tempprice = tempprice - 7500
      }
      this.setData({body:temppeople,price:tempprice})
    }
  },
  ldrpselect(e){
    const that = this
    let tempprice = 0
    if(this.data.ldrpselect){
      tempprice = this.data.price - 10000
      that.setData({ldrpselect:false,price:tempprice})
    }else if(!this.data.ldrpselect){
      tempprice = this.data.price + 10000
      that.setData({ldrpselect:true,price:tempprice})
    }
    // if(e.detail.value){
    //   tempprice = this.data.price + 10000
    // }else if(!e.detail.value){
    //   tempprice = this.data.price - 10000
    // }
    // this.setData({ldrpselect:e.detail.value,price:tempprice})
  },
  // multiple(e){
  //   this.setData({selectedmultiple:true})
  // },
  updateselect(e){
    const that = this
    let tempprice = 0
    if(this.data.updateselect){
      tempprice = this.data.price - 5000
      that.setData({updateselect:false,price:tempprice})
    }else if(!this.data.updateselect){
      tempprice = this.data.price + 5000
      that.setData({updateselect:true,price:tempprice})
    }
  },
  seebase(e){
    wx.navigateTo({
      url: '/pages/detail/detail?selectedweek=0&language=' + this.data.language + '&selectedserver=产检&selectedsetMeal=' + this.data.selectedsetMeal + '&selectedsetMealIndex=0&hide=true'
    })
  },
  seeroom(e){
    const that = this
    // console.log(that.data.selectedserver,that.data.selectedsetMealIndex)
    let tempsetMealIndex = undefined
    if(that.data.selectedsetMealIndex == 0){
      tempsetMealIndex = 1
    }else if(that.data.selectedsetMealIndex == 1){
      tempsetMealIndex = 2
    }else if(that.data.selectedsetMealIndex == 2){
      tempsetMealIndex = 1
    }
    if(that.data.selectedserver=="顺产"){
      wx.navigateTo({
        url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + that.data.selectedserver + '&selectedsetMeal=' + that.data.selectedsetMeal + '&selectedsetMealIndex=' + tempsetMealIndex
      })
    }else if(that.data.selectedserver=="剖腹产"){
      wx.navigateTo({
        url: '/pages/fertility/fertility?name=' + this.data.name + '&selectedweek=' + this.data.selectedweek + '&language=' + this.data.language + '&selectedserver=' + that.data.selectedserver + '&selectedsetMeal=' + that.data.selectedsetMeal + '&selectedsetMealIndex=' + tempsetMealIndex
      })
    }
  },
    multiple(e){
    const that = this
    if(!this.data.selectedmultiple){
      let tempprice = that.data.price + 20000
      if(that.data.selectedPrenatal){
        tempprice = tempprice + 7500
      }
      that.setData({selectedmultiple:true,price:tempprice})
    }else if(this.data.selectedmultiple){
      let tempprice = that.data.price - (this.data.body * 20000)
      if(that.data.selectedPrenatal){
        tempprice = tempprice - (that.data.body * 7500)
      }
      that.setData({selectedmultiple:false,price:tempprice,body:1})
    }
  },
  // clickLDPR(e){
  //   const that = this
  //   if(this.data.LDPRcheck){
  //     that.setData({LDPRcheck:false})
  //   }else if(!this.data.LDPRcheck){
  //     that.setData({LDPRcheck:true})
  //   }
  // }
  inputprompt(e){
    let tempstr = ""
    let length  = this.data.inputtext.length
    if(this.data.inputtext.length==0){
      let  serial = this.data.information.length+1
      tempstr =  serial + '.'
    }else{
      this.data.inputtext.forEach((e,index)=>{
          let  serial = this.data.information.length+index+1
          if(index!=(length-1)){
            tempstr = tempstr + serial + '.' + e + '\n'
          }else{
            tempstr = tempstr + serial + '.' + e
          }
      })
    }

    this.setData({ openinput: true,showinputtext:tempstr })
  },
  dismiss(e){
    this.setData({openinput:false})
  },
  inputstr(e){
    // console.log(e.detail.value)
    this.setData({inputvalue:e.detail.value})
  },
  confirm(e){
    this.setData({openinput:false})
  },
  prompt(e){
    let temparr = e.detail.value.split('\n')
    let temparr1 = []
    temparr.forEach(e=>{
      // if(e!=""){
        let tempstr = e.split('.').slice(1).join()
        if(tempstr != ""){
          temparr1.push(tempstr)
        }else{
          temparr1.push("")
        }
      // }
    })
    this.setData({ inputtext: temparr1})
  },
  getpdf(e){
    let data = this.data.infomations
    data.staff = app.globalData.staffId
    data.multiple = this.data.selectedmultiple
    data.selectedPrenatal = this.data.selectedPrenatal
    data.body = this.data.body
    data.ldrp = this.data.ldrpselect
    data.inputtext = this.data.inputtext
    data.price = this.data.price

    getpdf(data).then((res) => {
      // console.log(res.data)
      wx.downloadFile({
        url: res.data,
        success (res) {
          // console.log(res)
          wx.openDocument({
            filePath: res.tempFilePath,
          })
        }
      })
    })
  },
  dehide(e){
    // this.setData({selectedIndex:-1})
    this.data.deliverysetMealDetail[e.currentTarget.dataset.index].check = false
    this.setData({
      deliverysetMealDetail:this.data.deliverysetMealDetail
    })
  },
  demore(e){
    this.data.deliverysetMealDetail[e.currentTarget.dataset.index].check = true
    this.setData({
      deliverysetMealDetail:this.data.deliverysetMealDetail
    })
  },
  showprenatal(e){
    const that = this
    console.log(this.data.deliverysetMealDetail)
    if(this.data.showprenatal){
      that.setData({showprenatal:false})
    }else{
      this.data.deliverysetMealDetail.forEach(e=>{
        e.check = true
      })
      that.setData({showprenatal:true,deliverysetMealDetail:that.data.deliverysetMealDetail})
    }
  },
  selectedPrenatal(e){
    const that = this
    if(this.data.selectedPrenatal){
      let tempprice = that.data.price -25000
      if(that.data.selectedmultiple){
        tempprice = tempprice - (that.data.body * 7500)
      }
      that.setData({price:tempprice,selectedPrenatal:false})
    }else{
      let tempprice= that.data.price + 25000
      if(that.data.selectedmultiple){
        tempprice = tempprice + (that.data.body * 7500)
      }
      that.setData({price:tempprice,selectedPrenatal:true})
    }
  },
  linechange(e){
    const that = this
    // console.log(e)
    // console.log(this.data.inputtext)
    if(this.data.inputtext.length!=0){
      let tempshowtext = ""
      let length  = this.data.inputtext.length
      this.data.inputtext.forEach((e,index)=>{
        let  serial = this.data.information.length+index+1
        if(index!=(length-1)){
          tempshowtext = tempshowtext + serial + '.' + e + '\n'
        }else{
          tempshowtext = tempshowtext + serial + '.' + e
        }
      })
      this.setData({showinputtext:tempshowtext})
    }
  }
})